//
//  ByUIMineConsumeVC.m
//  chequanbao_ios
//
//  Created by mc on 2017/5/15.
//  Copyright © 2017年 com.itboye. All rights reserved.
//

#import "ByUIMineConsumeVC.h"

// cell
#import "ByCellMineConsume.h"

// api
#import "ByApiMine.h"

@interface ByUIMineConsumeVC ()<UITableViewDelegate,UITableViewDataSource>
// view
@property (nonatomic, strong) UITableView *tableView;

// data
@property (nonatomic, strong) NSMutableArray *dataAry;
@property (nonatomic, assign) NSInteger page_index;
@end

@implementation ByUIMineConsumeVC

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"我的消费记录";
    [self initTableView];
    [self.tableView.mj_header beginRefreshing];
    [self addLeftBackBarButtonItem];
}

#pragma mark -- getter
-(NSMutableArray *)dataAry{
    if (!_dataAry) {
        _dataAry = [NSMutableArray array];
    }
    return _dataAry;
}

#pragma mark -- 加载数据
- (void)fetchData{
    ByNetParamsModel *model = [[ByNetParamsModel alloc] init];
    model.uid = BY_USER.uid;
    model.page_index = [NSString stringWithFormat:@"%ld",self.page_index];
    model.page_size = @"10";
    [ByCoreToolAlert showLoadind:@"加载中..."];
    [ByApiMine buyRecord:model success:^(ByNetBaseResult *result) {
        NSInteger count = [result.data[@"count"] integerValue];
        NSMutableArray *temp = [NSMutableArray array];
        for (ByModelMineConsume *model in result.data[@"list"]) {
            [temp addObject:model];
        }
        if (self.page_index == 1) {
            [self.dataAry removeAllObjects];
            [self.dataAry addObjectsFromArray:[temp copy]];
            [self.tableView.mj_footer resetNoMoreData];
        }else{
            [self.dataAry addObjectsFromArray:[temp copy]];
        }
        [self.tableView reloadData];
        [ByCoreToolAlert hideHud];
        if (self.dataAry.count == count) {
            [self.tableView.mj_header endRefreshing];
            [self.tableView.mj_footer endRefreshingWithNoMoreData];
            return ;
        }
        [self.tableView.mj_header endRefreshing];
        [self.tableView.mj_footer endRefreshing];

    } failure:^(ByNetBaseResult *result) {
        [ByCoreToolAlert showOnlyText:result.msg];
    }];
}


#pragma mark -- 创建视图
- (void)initTableView{
    _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 64, SCREEN_WIDTH, SCREEN_HEIGHT - 64) style:UITableViewStyleGrouped];
    [self.view addSubview:_tableView];
    _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    _tableView.backgroundColor = [UIColor clearColor];
    _tableView.estimatedRowHeight = 0;
    _tableView.estimatedSectionHeaderHeight = 0;
    _tableView.estimatedSectionFooterHeight = 0;
    _tableView.delegate = self;
    _tableView.dataSource = self;
    
    ByCoreRefreshGifHeader *header = [ByCoreRefreshGifHeader headerWithRefreshingBlock:^{
        _page_index = 1;
        [self fetchData];
    }];
    self.tableView.mj_header = header;
    
    ByCoreRefreshGifFooter *footer = [ByCoreRefreshGifFooter footerWithRefreshingBlock:^{
        _page_index += 1;
        [self fetchData];
    }];
    self.tableView.mj_footer = footer;
    
}

#pragma mark -- UITableViewDelegate,UITableViewDataSource
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return self.dataAry.count;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 1;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    ByCellMineConsume *cell = [ByCellMineConsume cellWithTableView:tableView];
    cell.consume_model = self.dataAry[indexPath.section];
    return cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 65;
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 10;
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 0.001;
}
@end
